import axios from 'axios'
import qs from 'qs'
// import router from '../router'
import storejs from 'storejs'
import { message } from 'ant-design-vue'

const service = axios.create({
  baseURL: process.env.BASE_API,
  timeout: 5000,
  // 序列化params
  paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'repeat' })
})

// 请求拦截器
service.interceptors.request.use(config => {
  // config.data = JSON.stringify(config.data) // 数据转化,也可以使用qs转换
  // 发请求前做的一些处理，数据转化，配置请求头，设置token,设置loading等，根据需求去添加
  config.headers = {
    // 配置请求头
    'Content-Type': 'application/json',
    'Authorization': 'Basic c3dvcmQ6c3dvcmRfc2VjcmV0',
    'AppKey': '1a1bc0d811174dcda44b9b42b3c798ca',
    'Blade-Auth': 'bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOiJaSDIxMDQyMzA5MzY0OEY1ZTdiOCIsInVzZXJfbmFtZSI6IjEzNzU3MTUzNTQ3IiwiZ3JvdXBJZCI6IjEzODU0MTk3MzU4MjE4ODU0NDEiLCJhdmF0YXIiOiJodHRwczovL2d3LmFsaXBheW9iamVjdHMuY29tL3pvcy9ybXNwb3J0YWwvQmlhemZhbnhtYW1OUm94eFZ4a2EucG5nIiwiYXV0aG9yaXRpZXMiOlsiZGVmYWx1dCJdLCJjbGllbnRfaWQiOiJzYWJlciIsInJvbGVfbmFtZSI6ImRlZmFsdXQiLCJsaWNlbnNlIjoicG93ZXIgYnkgSnV4aXUiLCJ1c2VyX2lkIjoiMTM4NTUwOTk4ODQ1OTAxNjE5NCIsInJvbGVfaWQiOiIxMzAxNzkzMjc4NDg3Njk1MzYyIiwib3JnX2lkIjoxMzg1NDA3MjczNTg4NDAwMTMwLCJzY29wZSI6WyJhbGwiXSwibmlja19uYW1lIjoi5byg6aOe5bOwIiwiYXBwSWQiOiIxMzIzMDY4MzQwMDIxNzMxMzI5Iiwib2F1dGhfaWQiOiIiLCJleHAiOjE5ODc2MzY4NzAsImRlcHRfaWQiOiIiLCJqdGkiOiJmZDNiNTc3ZC1mYjhjLTQwMzktYWRkYS0wNjJhOTc3MGI2ZTUiLCJhY2NvdW50IjoiMTM3NTcxNTM1NDcifQ.02jNlxIhc-W5f43Mz6xu-ebtXNE76b3ILmVkX4XoYCw'
  }
  if (storejs.get('token')) {
    // config.headers.Authorization = storejs.get('token')
    config.headers.Authorization = 'Basic c3dvcmQ6c3dvcmRfc2VjcmV0'
  }
  return config
}, error => {
  // 将报错信息返回给catch捕获函数
  Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(response => {
  if (response.status === 200) {
    return response.data
  }
}, error => {
  // console.log(41, error)
  // /** *** 接收到异常响应的处理开始 *****/
  if (error && error.response) {
    // 1.公共错误处理
    // 2.根据响应码具体处理
    switch (error.response.status) {
      case 400:
        error.message = '错误请求'
        break
      case 401:
        error.message = '未授权，请重新登录'
        storejs.remove('token')
        // router.replace('/login')
        break
      case 403:
        error.message = '拒绝访问'
        break
      case 404:
        error.message = '请求错误,未找到该资源'
        // window.location.href = '/404'
        break
      case 405:
        error.message = '请求方法未允许'
        break
      case 408:
        error.message = '请求超时'
        break
      case 500:
        error.message = '服务器端出错'
        break
      case 501:
        error.message = '网络未实现'
        break
      case 502:
        error.message = '网络错误'
        break
      case 503:
        error.message = '服务不可用'
        break
      case 504:
        error.message = '网络超时'
        break
      case 505:
        error.message = 'http版本不支持该请求'
        break
      default:
        error.message = `连接错误${error.response.status}`
    }
  } else {
    // 超时处理
    if (JSON.stringify(error).includes('timeout')) {
      error.message = '服务器响应超时，请刷新当前页'
    } else {
      error.message = '连接服务器失败'
    }
  }
  message.error(error.message)
  // /** *** 处理结束 *****/
  // // 如果不需要错误处理，以上的处理过程都可省略
  return Promise.resolve(error.response)
})

export default service
